#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<numeric>
using namespace std;

class Solution {
public:
    int minOperations(vector<int>& nums, int x)
    {
        int sum = accumulate(nums.begin(), nums.end(), 0) - x;
        if (sum < 0)
            return -1;
        int ret = -1;
        for (int left = 0, right = 0, total = 0; right < nums.size(); right++)
        {
            total += nums[right];
            while (total > sum)
                total -= nums[left++];
            if (total == sum)
                ret = max(ret, right - left + 1);
        }
        return ret == -1 ? ret : nums.size() - ret;
    }
};